/*
 * create by yjp, 2017/3/8
 * 用户登录
 */
//-->手机号判断
// console.log(location.href);

function checkTel(phone){
	var myreg = /^(13|15|18|14|17)[0-9]{9}$/;
	if(phone==""||phone==null){
		$('.telErr').html('手机号码不能为空');
		return false;
	}else{
		$('.telErr').html('');
	}
	if(!myreg.test(phone)) {
		$('.telErr').html('手机号码格式错误');
		return false;
	}else{
		$('.telErr').html('');
	}
	return true;
}
//--手机登录
//登录数据判断
function loginDo(userPhone,userPwd,rmbpwd){
	//数据交互-验证手机号是否已注册；
	var data={
		'account':userPhone,
		'password':userPwd,
        'app_sid':'huoma'
	};
	$.ajax({
		url: top.server_url + "/users/login",
		type: 'POST',
		data: data,
		dataType: 'json',
		success: function(data){
			console.log(data);
			if(data.status == 200){
				if(rmbpwd){
                    // var Days = 2;
                    // var exp = new Date();
                    // exp.setTime(exp.getTime() + Days*24*60*60*1000);
                    // document.cookie="token="+data.payload.token;
                    setCookie('token',data.payload.token,2);
                    setCookie('account',data.payload.account,2);
                    // console.log(getCookie('token'))
				}
                if( data.payload.appInfo[0].vipInfo != undefined && data.payload.appInfo[0].vipInfo.huomaAmount != undefined && data.payload.appInfo[0].vipInfo.huomaAmount.can_use == 1) {
                    sessionStorage.setItem('user',data.payload.token);
                    sessionStorage.setItem('phone',data.payload.account);
                    sessionStorage.setItem('recommender',data.payload.appInfo[0].hasRecommender);
                    sessionStorage.setItem('uid',data.payload.uid);
                    top.location.href = "codemain.html";
				} else {
                    var r = confirm('您还未是会员，请购买会员！');
                    if(r) {
                        top.location.href = ''; //购买页面
                    }
                }
			}
			if(data.status == 400){
				$('.telErr').html(data.err);//传递错误信息
			}
		}
	});
}
function checkPwd(pwd){
    if( pwd=="" || pwd==null){
        $('.pwdErr').html("密码不能为空");
        return false;
    }
    $('.pwdErr').html('');
    return true;
}

//提交
    $('.login-button').click(function(){
            var userPhone = $("#userPhone").val();
            var userPwd = $("#userPwd").val();
            var rmbpwd = $("#weuiAgree").is(':checked');
            if(checkTel(userPhone)&&checkPwd(userPwd)){
                    loginDo(userPhone, userPwd,rmbpwd)
            }
        }

    )

// top.$(document).keyup(function(event) {
//         //获取当前按键的键值
//         //jQuery的event对象上有一个which的属性可以获得键盘按键的键值
//         var keycode = event.which;
//         var rmbpwd = $("#weuiAgree").attr('checked');
//         //处理回车的情况
//         if (keycode == 13) {
//             var userPhone = $("#userPhone").val();
//             var userPwd = $("#userPwd").val();
//             if(checkTel(userPhone)&&checkPwd(userPwd)){
//                 loginDo(userPhone, userPwd,rmbpwd)
//             }
//         }
//     }
// )
$(".forget-link").click(function () {
        location.href="find-password.html";
})
$(".register-link").click(function () {

        location.href="register.html";
})